'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 查询好友列表
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var heroConf = require('../../../../config/heroConf');
var Code = require('../../../../consts/code');
var dataApi = require('../../../../libs/dataApi');
var roleModel = require('../../../../models/roleModel');
var PlayerManager = require('../../../../libs/playerManager');
var friendService = require('../../../../services/friendService');
var vipConf = require('../../../../config/vipConf.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var friend_list;
    var friend_max_num;
    var friend_num;
    var receive_praise_times;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'friend', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var friend_model = player_manager.friend;
                var role_model = player_manager.role;

                var friend_role_id_list = friend_model.getFriendList();

                var my_vip_friend_num = vipConf.getConf(role_model.getVip()).add_friend_limit || 0;
                friend_max_num = heroConf.getFriendLimitNum(role_model.getLv()) + my_vip_friend_num;
                friend_num = friend_model.getFriendNum();
                receive_praise_times = friend_model.getReceivePraiseNum();

                // 获取所有好友信息
                friendService.getFriendList(friend_role_id_list, function (err, result) {
                    if (!!err) {
                        return cb();
                    }
                    friend_list = result;
                    cb();
                });
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    receive_praise_times: receive_praise_times,
                    friend_list: friend_list,
                    friend_num: friend_num,
                    friend_max_num: friend_max_num
                }
            });
        }
    );
};
